package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.LocationDelegate;
import coms.dto.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LocationAction extends ComsActionSupport implements IAction {

    private int locationId;
    private Location location;
    private List<Location> locationList;
    private LocationDelegate locationDelegate;
   
    private String action;
  
    
    public LocationAction() {
        location = new Location();
        locationDelegate = new LocationDelegate();
        locationList = new ArrayList<Location>();

    }

    private void initForm() {
       
    }
    
     private void populateDefault() {
        location = new Location();
  
    }
    
    private void populate(){
        try {
            location = locationDelegate.getById(locationId);
        } catch (Exception ex) {
            Logger.getLogger(LocationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {

        initForm();
        populateDefault();
        action = "createLocation_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updateLocation_" + location.getId();
        return "edit";
    }
    
    public String send(){
        populate();
        action = "sendLocation_" + location.getId();
        return "send";
    }

    @Override
    public String create() {

        locationDelegate.create(location);
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        try {
             locationDelegate.update(location);
        } catch (Exception ex) {
            Logger.getLogger(LocationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return "updated";
    }

    @Override
    public String delete() {
         populate();
        locationDelegate.delete(location);
        return "deleted";
    }

    @Override
    public String findAll() {
        try {
            locationList = locationDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(LocationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        try {
            locationList = locationDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(LocationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
  
        return "search";
    }
    
    
    public Location getLocation() {
        return location;
    }

    
    public void setLocation(Location location) {
        this.location = location;
    }

    public List<Location> getLocationList() {
        return locationList;
    }

    public void setLocationList(List<Location> locationList) {
        this.locationList = locationList;
    }

    public void setLocationId(String locationId) {
       try{
       this.locationId = Integer.parseInt(locationId);
       location.setId(this.locationId);
       } catch (Exception ex){
           Logger.getLogger(LocationAction.class.getName()).log(Level.SEVERE, null, ex);
       }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getLocationId() {
        return "" + locationId;
    }
    
}
